// --------------------------------------------------------------
// --------------------------------------------------------------


// -=-=--==-=-==--=-=-==--=-=-=-=-=-=-=--==-=-==--=-=-==--=-=-=-=-=-=-=--==-=-==--=-=-==--=-=-=-=-=
// 设置改变状态需要的几个常数
// 相当于宏定义
const MENU = 0;
const GAMING = 1;
const PAUSE = 2;
const OVER = 3;	

var marks = 0;

// 游戏的状态使用状态机实现:
// 每个状态需要继承并实现的函数如下.
function LogicStatus(lgc)
{
	var _this = this;
	
	// 保存logic指针用于使用logic的切换状态函数
	_this.logic = lgc;

	// 这个函数供logic调用. 也即通知状态对象: 现在要转到你执行了
	// 参数是上一个状态
	_this.prepare = function(preStat){}
	
	// 这个函数用来更新, 供logic调用
	_this.update = function(){}
	
	// 这个函数用来响应鼠标事件 
	_this.mouseClick = function(e){}
	
	// 这个函数用来响应键盘事件
	_this.keyDown = function(e){}
	
	// 这个函数用来清理资源
	_this.clearUp = function(){}
	// 这个函数用来查询ready状态.
	_this.ready = function(){}
}





// -=-=--==-=-==--=-=-==--=-=-=-=-=-=-=--==-=-==--=-=-==--=-=-=-=-=-=-=--==-=-==--=-=-==--=-=-=-=-=
// 游戏逻辑对象, 负责游戏的状态切换
// 参数: 传入一个SceneManager 提供对画图队列的操作
// 传入一个physics对象提供计算. 
function Logic(SceneM,PhysicS,GuI)
{

	
	var _this = this;
	_this.sceneManager = undefined;
	_this.physics = undefined;
	_this.gui = undefined;
	
	// 用于控制状态
	// 1.当前状态
	_this.curStatus = MENU;
	// 2.先前状态
	_this.preStatus = OVER;
	
	//-------------------
	// 游戏一共有4个状态. MENU, GAMING, PAUSE, OVER, 因此就需要4个状态类
	// 我在Init函数完成这些的初始化
	_this.menuStatus = undefined;
	_this.gamingStatus = undefined;
	_this.pauseStatus = undefined;
	_this.overStatus = undefined;
	
	_this.musicPlaying = true;
	// 用于计算时间.
	_this.time = 0;
	
	_this.update = function(time)
	{
		_this.time += time;
		//根据状态标志进入不同的状态
		switch(_this.curStatus) 
		{
			case MENU:   
				_this.menuStatus.update();
				break;
			case GAMING: 
				_this.gamingStatus.update();
				break;
			case PAUSE: 
				_this.pauseStatus.update();
				break;
			case OVER:
				_this.overStatus.update();
				break;			
		}
	}
	
	//状态改变函数
	_this.statusChange = function (newStatus)
	{
		switch(newStatus) 
		{
			case MENU:   
				_this.menuStatus.prepare(_this.curStatus);
				break;
			case GAMING: 
				_this.gamingStatus.prepare(_this.curStatus);
				break;
			case PAUSE: 
				_this.pauseStatus.prepare(_this.curStatus);
				break;
			case OVER:
				_this.overStatus.prepare(_this.curStatus);
				break;			
		}
		_this.preStatus = _this.curStatus;
		_this.curStatus = newStatus;
	}
	

	_this.mouseClick = function(e){
		switch(_this.curStatus) 
		{
			case MENU:   
				_this.menuStatus.mouseClick(e);
				break;
			case GAMING: 
				_this.gamingStatus.mouseClick(e);
				break;
			case PAUSE: 
				_this.pauseStatus.mouseClick(e);
				break;
			case OVER:
				_this.overStatus.mouseClick(e);
				break;			
		}
	}
	
	_this.keyDown = function(e){
		switch(_this.curStatus) 
		{
			case MENU:   
				_this.menuStatus.keyDown(e);
				break;
			case GAMING: 
				_this.gamingStatus.keyDown(e);
				break;
			case PAUSE: 
				_this.pauseStatus.keyDown(e);
				break;
			case OVER:
				_this.overStatus.keyDown(e);
				break;			
		}
	}
	
	_this.keyUp = function(e){  
        _this.gamingStatus.keyUp(e);
    }
	
	_this.mouseMove = function(e)
    {
            switch(_this.curStatus) 
        {
            case MENU:   
                _this.menuStatus.mouseMove(e);
                break;
            case GAMING: 
                _this.gamingStatus.mouseMove(e);
                break;
            case PAUSE: 
                _this.pauseStatus.mouseMove(e);
                break;
            case OVER:
                _this.overStatus.mouseMove(e);
                break;          
        }
    }
	
	// 像Logic Physics类都必须提供一个ready方法确保能够正常工作
	_this.ready = function ()
	{
		// 确认所有的状态类都已经准备完成:
		return _this.menuStatus.ready()
			 && _this.gamingStatus.ready()
			 && _this.pauseStatus.ready()
			 && _this.overStatus.ready();
	}
	
	// 初始化啊初始化
	var init = function(){
		_this.physics = PhysicS;
		_this.sceneManager = SceneM;
		_this.gui = GuI
		
		// ----------------------留给阿吮参考-----------------------
		//var gB = _this.gui.createController("GButton",new Vector2(0,0), new Rectangle(100,100));
//		gB.imgNormal.src = "./images/1.png";
//		gB.imgOnFocus.src = "./images/2.png";
//		gB.imgOnPress.src = "./images/3.png";
//		gB.onMouseClick = function(){ alert("阿吮啊阿吮, 弱爆了");_this.gui.deleteController(gB);}
//		_this.gui.addController(gB);
		
		var tB = _this.gui.createController("GToggleButton",new Vector2(440,10), new Rectangle(32,32));
		tB.imgFront.src = "./images/trumpet_on.png";
		tB.imgBack.src = "./images/trumpet_off.png";
		tB.imgOnFocus.src = "./images/trumpet_h1.png";
		tB.imgOnPress.src = "./images/trumpet_h1.png";
		tB.onMouseClick = 
		function() {
			var mAudio = document.getElementById('myaudio');
			if (mAudio.paused) {
					mAudio.play();
					tB.imgOnFocus.src = "./images/trumpet_h1.png";
					tB.imgOnPress.src = "./images/trumpet_h1.png";
				}
				else {
					mAudio.pause();
					tB.imgOnFocus.src = "./images/trumpet_h2.png";
					tB.imgOnPress.src = "./images/trumpet_h2.png";
				}
			tB.toggle();
		}
		_this.gui.addController(tB);
		
		// --------------------参考完了就发挥聪明才智想办法加到各个Status里面吧---------------------------
			
		
		
		
		// 绑定canvas的按键事件
		_this.sceneManager.canvas.addEventListener("click",_this.mouseClick,false);
		_this.sceneManager.canvas.addEventListener("mousemove",_this.mouseMove,false);
		// 绑定窗口的案件事件
		document.onkeydown = _this.keyDown;
		document.onkeyup = _this.keyUp;
		   
		_this.menuStatus = new MenuStatus(_this);
		_this.gamingStatus = new GamingStatus(_this);
		_this.pauseStatus = new PauseStatus(_this);
		_this.overStatus = new OverStatus(_this);

	}
	init();
}

// --------------------------------------------------------------
// --------------------------------------------------------------
